package com.aaron.kjviewutils.wechatsift;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aaron.kjviewutils.MobDetailActivity;
import com.aaron.kjviewutils.R;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：Aaron on 2017/2/23 15:46
 * 邮箱：kaijin.fang@edianniu.com
 */
public class WeChatAdapter extends RecyclerView.Adapter<WeChatAdapter.WeChatViewHolder> {

    private List<WeChatCatagoryBean.WeChatCatagoryInner.WeChatCatagoryInfo> datas;
    private Context context;
    private Activity aty;

    public WeChatAdapter(Activity aty, Context context, List<WeChatCatagoryBean.WeChatCatagoryInner.WeChatCatagoryInfo> datas)
    {
        this.aty = aty;
        this.context = context;
        this.datas = datas;
    }

    @Override
    public WeChatViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_wechat, parent, false);
        return new WeChatViewHolder(view);
    }

    @Override
    public void onBindViewHolder(WeChatViewHolder holder, int position) {
        if(null!=datas&&!datas.isEmpty()&&null!=datas.get(position))
        {
            final WeChatCatagoryBean.WeChatCatagoryInner.WeChatCatagoryInfo weChatCatagory = datas.get(position);
            holder.title.setText(weChatCatagory.title);
            holder.pubTime.setText("发布时间:" + weChatCatagory.pubTime);
            holder.subTitle.setText(weChatCatagory.subTitle);
            holder.sourceUrlL.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString("title", weChatCatagory.title);
                    bundle.putString("url", weChatCatagory.sourceUrl);
                    ((WeChatSiftActivity)aty).showActivity(aty, MobDetailActivity.class, bundle);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return (null==datas)?0:datas.size();
    }

    public static class WeChatViewHolder extends RecyclerView.ViewHolder
    {
        @Bind(R.id.title)
        TextView title;
        @Bind(R.id.pubTime)
        TextView pubTime;
        @Bind(R.id.subTitle)
        TextView subTitle;
        @Bind(R.id.sourceUrlL)
        RelativeLayout sourceUrlL;

        public WeChatViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
